﻿from random import randint
from time import sleep
from selenium.webdriver.support import expected_conditions as ec
from selenium import webdriver
from HTMLTestRunner import HTMLTestRunner
import unittest

from selenium.webdriver.support.select import Select


class JCX_Test(unittest.TestCase):
    driver = webdriver.Firefox()

    def test_JCX_Login(self):
        self.driver.get('http://localhost/woyaogongjizhizhua/index.asp')
        sleep(1)
        self.driver.find_element_by_name('username').send_keys('admin')
        self.driver.find_element_by_name('pwd').send_keys('admin')
        self.driver.find_element_by_name('enter').click()
        sleep(1)
        self.assertIn('后台管理系统', self.driver.page_source)

    def test_JCX_DelUnit(self):
        sleep(1)
        self.driver.switch_to.frame('left')
        sySet = '.menuall > tbody:nth-child(1) > tr:nth-child(13) > td:nth-child(1) > img:nth-child(1)'
        self.driver.find_element_by_css_selector(sySet).click()
        sleep(1)
        JlDwSet = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(7) > td:nth-child(1) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2)'
        self.driver.find_element_by_css_selector(JlDwSet).click()
        sleep(1)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')
        self.driver.find_element_by_name('chkall').click()
        DelSet = 'input.button:nth-child(2)'
        self.driver.find_element_by_css_selector(DelSet).click()
        sleep(2)
        self.driver.switch_to.alert.accept()
        sleep(1)
        self.assertNotIn('name="ID"', self.driver.page_source)

    def test_JXC_AddUnit(self):
        bt_set = 'input.button:nth-child(1)'
        self.driver.find_element_by_css_selector(bt_set).click()
        self.driver.find_element_by_name('danwei').send_keys('t')
        sleep(1)
        self.driver.find_element_by_name('submit').click()
        sleep(1)
        self.driver.switch_to.alert.accept()
        sleep(1)
        self.assertIn('t', self.driver.page_source)

    def test_JXC_AddDL(self):
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('left')
        CpDl_Set = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(5) > td:nth-child(1) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2)'
        self.driver.find_element_by_css_selector(CpDl_Set).click()
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')
        sleep(1)
        TjDl_Set = 'input.button:nth-child(1)'
        self.driver.find_element_by_css_selector(TjDl_Set).click()
        self.driver.find_element_by_name('bigclass').send_keys('军事')
        sleep(1)
        self.driver.find_element_by_name('submit').click()
        sleep(1)
        self.driver.switch_to.alert.accept()
        sleep(1)
        self.assertIn('军事', self.driver.page_source)

    def test_JXC_AddXL(self):
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('left')
        CpXl_Set = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(6) > td:nth-child(1) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2)'
        self.driver.find_element_by_css_selector(CpXl_Set).click()
        sleep(1)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')
        sleep(1)
        TjXl_Set = 'input.button:nth-child(1)'
        self.driver.find_element_by_css_selector(TjXl_Set).click()
        Q = '.toptable > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2) > select:nth-child(1)'
        sel = self.driver.find_element_by_css_selector(Q)
        lst = Select(sel)
        lst_count = len(lst.options)
        rand = randint(0, lst_count - 1)
        lst.select_by_index(rand)
        sleep(1)
        self.driver.find_element_by_name('smallclass').send_keys('汽车')
        sleep(1)
        self.driver.find_element_by_name('submit').click()
        sleep(1)
        self.driver.switch_to.alert.accept()
        sleep(1)
        self.assertIn('汽车', self.driver.page_source)

    def test_JXC_SafeQuit(self):
        self.driver.switch_to.default_content()
        # 切换到顶部框架
        self.driver.switch_to.frame('topFrame')
        self.driver.find_element_by_link_text('安全退出').click()
        sleep(1)
        alt = ec.alert_is_present()(self.driver)
        if alt:
            alt.accept()
        sleep(3)
        self.driver.switch_to.default_content()
        self.assertIn('系统登陆', self.driver.page_source)

    def test_quit(self):
        self.driver.quit()


def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(JCX_Test('test_JCX_Login'))
    suite.addTest(JCX_Test('test_JCX_DelUnit'))
    suite.addTest(JCX_Test('test_JXC_AddUnit'))
    suite.addTest(JCX_Test('test_JXC_AddDL'))
    suite.addTest(JCX_Test('test_JXC_AddXL'))
    suite.addTest(JCX_Test('test_JXC_SafeQuit'))
    suite.addTest(JCX_Test('test_quit'))
    return suite


with open('d:/JXC_test.html', 'wb') as f:
    runner = HTMLTestRunner(stream=f, title='进存销测试报告', description='报告人：科171刘畅 _20200622')
    runner.run(test_suite())
